﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

namespace GeniusAdminWebApplication
{
    public partial class admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IList<User> adminList = getAdminList();

            ActionService actionService = new ActionService();
            IList<Model.Action> actionList = actionService.getActionList();
            UserActionService usa = new UserActionService();

            foreach (User u in adminList)
            {
                Panel p_item = new Panel();
                p_item.CssClass = "action_list";
                p_item.Controls.Add(new LiteralControl("<span class='action_userId'>" + u.id + "</span>"));

                //遍历权限
                foreach (Model.Action action in actionList)
                {
                    if (usa.getUserAction(u, action.id) != null)
                    {
                        p_item.Controls.Add(new LiteralControl("<span class='action_user'><input id='check" + u.id + action.id + "' type='checkbox' checked /></span>"));
                    }
                    else
                    {
                        p_item.Controls.Add(new LiteralControl("<span class='action_user'><input id='check" + u.id + action.id + "' type='checkbox' /></span>"));
                    }
                }
                p_item.Controls.Add(new LiteralControl("<span class='action_operate'><input type='button' value='确认修改' onclick='modifyUserAction(" + u.id + ",\"check" + u.id + "\");'/>"));
                p_item.Controls.Add(new LiteralControl("<input type='button' value='删除管理员' onclick=\"deleteAdmin(" + u.id + ");\"/></span>"));
                ph_admin.Controls.Add(p_item);

            }
        }

        private IList<User> getAdminList()
        {
            AdminService adminService = new AdminService();
            return adminService.getAdminUser();
        }

        //添加管理员
        [WebMethod]
        public static string ajaxAddAdmin(string userId)
        {
            int id;
            try
            {
                id = int.Parse(userId);
            }
            catch
            {
                return "{" +
                            "result:'" + 0 + "'," +
                            "message:'请输入整数'" +
                        "}";
            }
            AdminService adminService = new AdminService();
            Hashtable ht = adminService.changeToAdmin(id);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            new SystemLogService().saveSystemLog((User)HttpContext.Current.Session["admin"], "添加管理员 ID：" + id);
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }


        //修改权限
        [WebMethod]
        public static string ajaxModifyAdminAction(int userId, string actionStr)
        {
            string[] actionArray = actionStr.Split(new char[1] { '#' });
            int action1 = int.Parse(actionArray[0]);
            int action2 = int.Parse(actionArray[1]);
            int action3 = int.Parse(actionArray[2]);
            UserActionService usa = new UserActionService();
            Hashtable ht = usa.modifyUserAction(userId, action1, action2, action3);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

        //删除管理员
        [WebMethod]
        public static string ajaxDeleteAdmin(int userId)
        {
            AdminService adminService = new AdminService();
            Hashtable ht = adminService.deleteAdmin(userId);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

    }
}